﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Caliburn.Core;
using Caliburn.PresentationFramework;
using Caliburn.PresentationFramework.ApplicationModel;
using XWord.Fundamental.Domain;
using System.IO;
using Microsoft.Practices.ServiceLocation;
using Castle.Windsor;
using XWord.WPF.ApplicationFramework;
using System.Reflection;
using XWord.GUI.Presenters;
using Caliburn.Castle;
using Luna.WPF.ApplicationFramework;
using Microsoft.Practices.Composite.Modularity;
using XWord.GUI.Presenters.Fundamental;
using System.Windows.Threading;
using XWord.GUI.Presenters.Interface;
using XWord.GuyWire;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Data;
using System.Diagnostics;
using XWord.Fundamental.Domain.Services;
using System.Windows.Interactivity;

namespace XWord.GUI
{

    public partial class App : BootstrapperApplication
    {


        protected override object CreateRootModel()
        {
            return Container.GetInstance<IDockSitePresenter>();
        }

        protected override IServiceLocator CreateContainer()
        {
            var containerAccessor = new GeneralGuyWire();
            var windsorAdapter = new WindsorAdapter(containerAccessor.Container);
            containerAccessor.Wire();
            return windsorAdapter;
        }

        protected override void ShowMainWindow(object rootModel)
        {
            base.ShowMainWindow(rootModel);
            RibbonDockWindowCycle.Run();
            Container.GetInstance<IEtymaWordListPresenter>().Initialize();
        }

        protected override void OnStartup(StartupEventArgs e)
        {
          
            LoadModuleAssemblies();
            //LoadDefaultTrayIcon();
            //LoadTrayIconMenu();
            base.OnStartup(e);
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += delegate {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            //    SaveData();
            };
            timer.Start();
            ConfigMainWindow();
            this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
        }

         public static BootstrapperApplication CurrentApplication
        {
            get
            {
                return Application.Current as BootstrapperApplication;
            }
        }

         #region TrayIcon

         private void LoadTrayIcon()
         {
             LoadDefaultTrayIcon();
             LoadTrayIconMenu();
         }

         public static void LoadDefaultTrayIcon()
         {
             BitmapImage bi = new BitmapImage();
             bi.BeginInit();
             bi.UriSource = new Uri(@"pack://application:,,,/Resources/Images/ActiproIcon.ico", UriKind.RelativeOrAbsolute);
             bi.EndInit();
             CurrentApplication.TrayIcon.Icon = bi;
         }

         public static void LoadTrayIconMenu()
         {
             CurrentApplication.TrayIcon.ContextMenu = CurrentApplication.FindResource("TrayIconMenu") as ContextMenu;
             Caliburn.PresentationFramework.Actions.Action.SetTargetWithoutContext(
                 CurrentApplication.TrayIcon.ContextMenu,
                 ServiceLocator.Current.GetInstance<object>("TrayIconPresenter"));
         }

         #endregion

         private void ConfigMainWindow()
         {
             var window = Application.Current.MainWindow;
             var behaviors = Interaction.GetBehaviors(window);
             behaviors.Add(new WindowInitBehavior());
         }

        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
        }

        protected override void OnExit(ExitEventArgs e)
        {
            SaveData();
            base.OnExit(e);
        }

        private void SaveData()
        {
            //var list = ServiceLocator.Current.GetInstance<WordExplorerPresenter>().AllWordList;
            //BinaryFormatterHelper.Serialize("AllWordList.data", list);
            //var list2 = ServiceLocator.Current.GetInstance<EtymaLibraryExplorerPresenter>().AllEtymaLibraryList;
            //BinaryFormatterHelper.Serialize("AllEtymaLibraryList.data", list2);
            XWordAppConfig.Current.Save();
           // RemotingServiceFactory.GetWordService().AutoUpdate();
        }
    }
}
